package cn.livingCloud.service.impl;

import cn.livingCloud.dto.ArticleDto;
import cn.livingCloud.dto.ArticleFormDto;
import cn.livingCloud.dto.ArticleJsonDto;
import cn.livingCloud.dto.ArticleListDto;
import cn.livingCloud.dto.front.ArticleFrontDto;
import cn.livingCloud.dto.front.ArticleFrontListDto;
import cn.livingCloud.entity.content.Article;
import cn.livingCloud.repository.ArticleMapper;
import cn.livingCloud.service.ArticleService;
import cn.livingCloud.service.impl.operation.ArticleFormDtoUpdater;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DingJie  on 2016/1/15.
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper mapper;

    @Override
    public void getArticleList(ArticleListDto listDto) {
        List<Article> articleList = mapper.getArticleList(listDto);
        listDto.addAllList(articleList);
    }

    @Override
    public ArticleDto uploadState(String uuid, boolean publish) {
        Article article = mapper.findByUuid(uuid);
        //如果前台传入执行的状态和数据库的状态相同那么就抛出异常
        if (publish == article.isPublish()) {
            throw new IllegalStateException("禁用/启用失败,请检查账号状态是否被禁用/启用!");
        }
        //如果前台的状态和数据库的状态不相同那么就执行当前传入的操作
        article.setPublish(publish);
        mapper.updatePublish(article);
        return new ArticleDto(article);
    }

    @Override
    public ArticleFormDto loadArticleFromDto(String uuid) {
        if (StringUtils.isEmpty(uuid)) {
            return new ArticleFormDto();
        }
        Article byUuid = mapper.findByUuid(uuid);
        return new ArticleFormDto(byUuid);
    }

    @Override
    public void edit(ArticleFormDto formDto) {
        ArticleFormDtoUpdater updater = new ArticleFormDtoUpdater(formDto);
        updater.update();
    }

    @Override
    public void delete(String uuid) {
        mapper.delete(uuid);
    }

    @Override
    public ArticleFrontDto getArticleFrontDto(String uuid) {
        Assert.notNull(uuid);
        Article article = mapper.findByUuid(uuid);
        return new ArticleFrontDto(article);
    }

    @Override
    public void getArticleFrontListDto(ArticleFrontListDto frontListDto) {
        frontListDto.addAllList(mapper.getArticlFronteList(frontListDto));
//        for (ArticleDto articleDto : frontListDto.getList()) {
//            if (articleDto.getTitle().length() > 18) {
//                articleDto.setTitle(articleDto.getTitle().substring(0,18)+"..");
//            }
//        }
    }

    @Override
    public List<ArticleJsonDto> searchList(String key) {
        List<ArticleJsonDto> jsonDtos = new ArrayList<>();
        List<Article> articles = mapper.findArticleList(key);
        for (Article article : articles) {
            jsonDtos.add(new ArticleJsonDto(article));
        }
        return jsonDtos;
    }
}
